﻿@using Flagrum.Core.Utilities
@using Flagrum.Web.Features.AssetExplorer.Data
@using Flagrum.Web.Persistence.Entities
@inject IWpfService WpfService
@inject FlagrumDbContext Context
@inject AppStateService AppState
@inject IStringLocalizer<ModelPreviewSettingsModal> Localizer
@inject IStringLocalizer<App> AppLocalizer

<AutosizeModal @ref="Modal">
    <HeaderView>
        <span class="text-grey-300 flex-grow">@Localizer["Title"]</span>
        <span class="material-icons cursor-pointer" @onclick="Cancel">close</span>
    </HeaderView>
    <BodyView>
        <div>
            <div class="row mb-3">
                <h5 class="text-accent1-200">Key Bindings</h5>
            </div>
            <div class="row mb-3">
                <label class="text-right pr-3" style="width: 190px">@Localizer["Rotate"]</label>
                <select @bind="RotateModifierKey" class="input bg-dark-550 w-full">
                    @foreach (var key in ModifierKeys)
                    {
                        <option value="@key">@key.SpacePascalCase()</option>
                    }
                </select>
                <strong class="mx-3">+</strong>
                <select @bind="RotateMouseAction" class="input bg-dark-550 w-full">
                    @foreach (var key in MouseActions)
                    {
                        <option value="@key">@key.SpacePascalCase()</option>
                    }
                </select>
            </div>
            <div class="row mb-8">
                <label class="text-right pr-3" style="width: 190px">@Localizer["Pan"]</label>
                <select @bind="PanModifierKey" class="input bg-dark-550 w-full">
                    @foreach (var key in ModifierKeys)
                    {
                        <option value="@key">@key.SpacePascalCase()</option>
                    }
                </select>
                <strong class="mx-3">+</strong>
                <select @bind="PanMouseAction" class="input bg-dark-550 w-full">
                    @foreach (var key in MouseActions)
                    {
                        <option value="@key">@key.SpacePascalCase()</option>
                    }
                </select>
            </div>
            <div class="row mb-3">
                <h5 class="text-accent1-200">Texture Settings</h5>
            </div>
            <div class="row mb-8">
                <label class="text-right pr-3">Texture Fidelity</label>
                <EditForm Model="this">
                    <EnumSelect TEnum="ModelViewerTextureFidelity" @bind-Value="TextureFidelity"/>
                </EditForm>
            </div>
            <Button Icon="save" Text=@AppLocalizer["Save"] OnClick="Save"/>
        </div>
    </BodyView>
</AutosizeModal>

@code
{
    [CascadingParameter]
    public ModelPreview Parent { get; set; }

    private AutosizeModal Modal { get; set; }

    private string RotateModifierKey { get; set; }
    private string RotateMouseAction { get; set; }
    private string PanModifierKey { get; set; }
    private string PanMouseAction { get; set; }
    private List<string> ModifierKeys { get; set; } = new();
    private List<string> MouseActions { get; set; } = new();
    private ModelViewerTextureFidelity TextureFidelity { get; set; }

    protected override void OnInitialized()
    {
        ModifierKeys = WpfService.GetModifierKeys().ToList();
        MouseActions = WpfService.GetMouseActions().ToList();
    }

    public void Open()
    {
        RotateModifierKey = Context.GetString(StateKey.ViewportRotateModifierKey);
        RotateMouseAction = Context.GetString(StateKey.ViewportRotateMouseAction);
        PanModifierKey = Context.GetString(StateKey.ViewportPanModifierKey);
        PanMouseAction = Context.GetString(StateKey.ViewportPanMouseAction);
        var textureFidelity = Context.GetInt(StateKey.ViewportTextureFidelity);
        TextureFidelity = textureFidelity == -1 ? ModelViewerTextureFidelity.Low : (ModelViewerTextureFidelity)textureFidelity;

        AppState.IsModalOpen = true;
        WpfService.Set3DViewportVisibility(false);
        Modal.Open();
    }

    private void Cancel()
    {
        Modal.Close();
        AppState.IsModalOpen = false;

        if (AppState.Is3DViewerOpen)
        {
            WpfService.Set3DViewportVisibility(true);
        }
    }

    private void Save()
    {
        Context.SetString(StateKey.ViewportRotateModifierKey, RotateModifierKey);
        Context.SetString(StateKey.ViewportRotateMouseAction, RotateMouseAction);
        Context.SetString(StateKey.ViewportPanModifierKey, PanModifierKey);
        Context.SetString(StateKey.ViewportPanMouseAction, PanMouseAction);
        Context.SetInt(StateKey.ViewportTextureFidelity, (int)TextureFidelity);

        WpfService.Update3DViewportBindings(RotateModifierKey, RotateMouseAction, PanModifierKey, PanMouseAction);

        Cancel();

        Parent.CallStateHasChanged();
    }
}